//
//  XMDateSelectView.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/14.
//

#import "XMDateSelectView.h"

@interface XMDateSelectView ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) UIView *bodyView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIDatePicker *datePicker;
@property (nonatomic, strong) UIPickerView *pickerView;

@property (nonatomic, copy) NSString *currentYear;

@property (nonatomic, assign) NSInteger selectMonth;
@property (nonatomic, assign) BOOL isFebruary;
@property (nonatomic, assign) BOOL isLeapYear;

@end

@implementation XMDateSelectView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = [UIColor clearColor];
    _currentYear = [[NSDate date] dateStringWithFormat:@"yyyy"];
    [self addSubview:self.bodyView];
    [self addSubview:self.contentView];
    [_contentView addSubview:self.pickerView];
    [_contentView addSubview:self.datePicker];
    _datePicker.hidden = YES;
    _bodyView.alpha = 0;
    [_pickerView selectRow:49 inComponent:0 animated:YES];
}

- (void)confirmAction:(UIButton *)button {
    [_pickerView selectedRowInComponent:0];
    NSString *selectDateStr = [NSString stringWithFormat:@"%02ld/%02ld/%02ld", [_pickerView selectedRowInComponent:0] + 1, [_pickerView selectedRowInComponent:1] + 1, [_pickerView selectedRowInComponent:2]];
    if (_dateType == XMDateDateTimeType) {
//        selectDateStr = [NSString stringWithFormat:@"%ld.%02ld.%02ld %02ld:%02ld:%02ld", [_currentYear intValue] - 50 + [_pickerView selectedRowInComponent:0] + 1, [_pickerView selectedRowInComponent:1] + 1, [_pickerView selectedRowInComponent:2] + 1, [_pickerView selectedRowInComponent:3], [_pickerView selectedRowInComponent:4], [_pickerView selectedRowInComponent:5]];
        selectDateStr = [NSString stringWithFormat:@"%ld.%02ld.%02ld %02ld:%02ld", [_currentYear intValue] - 50 + [_pickerView selectedRowInComponent:0] + 1, [_pickerView selectedRowInComponent:1] + 1, [_pickerView selectedRowInComponent:2] + 1, [_pickerView selectedRowInComponent:3], [_pickerView selectedRowInComponent:4]];
    }
    self.dateSelectBlock ? self.dateSelectBlock(selectDateStr) : nil;
    [self disMiss];
}

- (void)cancelAction:(UIButton *)button {
    [self disMiss];
}

- (void)show {
//    _pickerView.hidden = (_dateType == XMDateDateTimeType);
//    _datePicker.hidden = !(_dateType == XMDateDateTimeType);
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.bodyView.alpha = 1;
        weakSelf.contentView.y = weakSelf.height - weakSelf.contentView.height;
    }];
}

- (void)disMiss {
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.bodyView.alpha = 0;
        weakSelf.contentView.y = weakSelf.height;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

#pragma mark - Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if (_dateType == XMDateDateTimeType) {
        return 5;
    }
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    switch (component) {
        case 0: {
            return 100;
        }
            break;
        case 1: {
            return 12;
        }
            break;
        case 2: {
            if (_isFebruary) {
                return 28 + (_isLeapYear ? 1 : 0);
            }
            else {
                if ([@[@"4", @"6", @"9", @"11"] containsObject:[NSString stringWithFormat:@"%ld", _selectMonth]]) {
                    return 30;
                }
                return 31;
            }
        }
            break;
        case 3: {
            return 24;
        }
            break;
        case 4: {
            return 60;
        }
            break;
        case 5: {
            return 60;
        }
            break;
            
        default:
            break;
    }
//    if (component == 0) {
//        return 150;
//    }
//    else if (component == 1) {
//        return 12;
//    }
//    else if (component == 2) {
//        if (_isFebruary) {
//            return 28 + (_isLeapYear ? 1 : 0);
//        }
//        else {
//            if ([@[@"4", @"6", @"9", @"11"] containsObject:[NSString stringWithFormat:@"%ld", _selectMonth]]) {
//                return 30;
//            }
//            return 31;
//        }
//    }
    return 0;
}

//- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
//    if (component == 0) {
//        return self.width/4;
//    }
//    else {
//        return self.width/6;
//    }
//}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return KScaleWidth(45.0);
}

//- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
//    NSString *str = @"";
//    if (component == 0) {
//        str = @"0000年";
//    }
//    else {
//        str = @"00月";
//    }
//    if (component == 0) {
//        return KScaleWidth(60.0)*2;
//    }
//    else {
//        return KScaleWidth(30.0)*2;
//    }
////    CGFloat width = [str getWidthWithFont:KScaleWidth(18.0)];
////    return width;
//}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    NSString *str = @"";
    if (component == 0) {
        str = @"0000年";
    }
    else {
        str = @"00秒";
    }
    CGFloat width = [str getWidthWithFont:KScaleWidth(18.0)] + KScaleWidth(5.0);
//    if (component == 0) {
//        width = self.width/7*2;
//    }
//    else {
//        width = self.width/7;
//    }
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, KScaleWidth(45.0)) font:KFontSize(18.0) textColor:kColorHEX(0x333333) textAlignment:NSTextAlignmentCenter];
//    titleLabel.backgroundColor = kColorHEX(0x00ffff);
    
    switch (component) {
        case 0: {
            str = [NSString stringWithFormat:@"%ld年", [_currentYear intValue] - 50 + row + 1];
        }
            break;
        case 1: {
            str = [NSString stringWithFormat:@"%ld月", row + 1];
        }
            break;
        case 2: {
            str = [NSString stringWithFormat:@"%ld日", row + 1];
        }
            break;
        case 3: {
            str = [NSString stringWithFormat:@"%ld时", row];
        }
            break;
        case 4: {
            str = [NSString stringWithFormat:@"%ld分", row];
        }
            break;
        case 5: {
            str = [NSString stringWithFormat:@"%ld秒", row];
        }
            break;
        default: {
            
        }
            break;
    }
    titleLabel.text = str;
    return titleLabel;
}

//- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    NSString *str = @"";
//    switch (component) {
//        case 0: {
//            str = [NSString stringWithFormat:@"%ld年", [_currentYear intValue] - 150 + row + 1];
//        }
//            break;
//        case 1: {
//            str = [NSString stringWithFormat:@"%ld月", row + 1];
//        }
//            break;
//        case 2: {
//            str = [NSString stringWithFormat:@"%ld日", row + 1];
//        }
//            break;
//        case 3: {
//            str = [NSString stringWithFormat:@"%ld时", row + 1];
//        }
//            break;
//        case 4: {
//            str = [NSString stringWithFormat:@"%ld分", row + 1];
//        }
//            break;
//        case 5: {
//            str = [NSString stringWithFormat:@"%ld秒", row + 1];
//        }
//            break;
//        default: {
//
//        }
//            break;
//    }
//    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName : KFontSize(12.0), NSForegroundColorAttributeName : kColorHEX(0x00ffff)}];
//    return attr;
//}

//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    switch (component) {
//        case 0: {
//            NSString *yearStr = [NSString stringWithFormat:@"%ld年", [_currentYear intValue] - 150 + row + 1];
//            return yearStr;
//        }
//            break;
//        case 1: {
//            return [NSString stringWithFormat:@"%ld月", row + 1];
//        }
//            break;
//        case 2: {
//            return [NSString stringWithFormat:@"%ld日", row + 1];
//        }
//            break;
//        case 3: {
//            return [NSString stringWithFormat:@"%ld时", row + 1];
//        }
//            break;
//        case 4: {
//            return [NSString stringWithFormat:@"%ld分", row + 1];
//        }
//            break;
//        case 5: {
//            return [NSString stringWithFormat:@"%ld秒", row + 1];
//        }
//            break;
//        default: {
//            return @"";
//        }
//            break;
//    }
////    if (component == 0) {
////        NSString *yearStr = [NSString stringWithFormat:@"%ld年", [_currentYear intValue] - 150 + row + 1];
////        return yearStr;
////    }
////    else if (component == 1) {
////        return [NSString stringWithFormat:@"%ld月", row + 1];
////    }
////    else {
////        return [NSString stringWithFormat:@"%ld日", row + 1];
////    }
//}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        [self getYearTypeWithIndex:row];
        [pickerView reloadComponent:1];
        [pickerView reloadComponent:2];
    }
    else if (component == 1) {
        _selectMonth = row + 1;
        _isFebruary = (row == 1);
        [pickerView reloadComponent:2];
    }
}

//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
//
//}

#pragma mark - Init
- (UIView *)bodyView {
    if (!_bodyView) {
        _bodyView = [[UIView alloc] initWithFrame:self.bounds];
        _bodyView.backgroundColor = kColorHEXA(0x464646, 0.46);
    }
    return _bodyView;
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height, self.width, KScaleWidth(365.0))];
        _contentView.backgroundColor = kColorHEX(0xffffff);
        [_contentView setPartCornerWithRectCorner:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadius:KScaleWidth(12.0)];
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake(0, KScaleWidth(16.0), KScaleWidth(47.0) + KScaleWidth(20.0), KScaleWidth(42.0)) title:@"取消" titleColor:kColorHEX(0x666666)];
        [cancelBtn addTarget:self action:@selector(cancelAction:) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:cancelBtn];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(cancelBtn.width, cancelBtn.y, _contentView.width - cancelBtn.width*2, cancelBtn.height) font:KFontSize(18.0) textColor:kColorHEX(0x333333) textAlignment:NSTextAlignmentCenter];
        titleLabel.text = @"选择你的生日";
        [_contentView addSubview:titleLabel];
        
        UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake(_contentView.width - cancelBtn.width, cancelBtn.y, cancelBtn.width, cancelBtn.height) title:@"保存" titleColor:kColorHEX(0x3B5998)];
        [confirmBtn addTarget:self action:@selector(confirmAction:) forControlEvents:UIControlEventTouchUpInside];
        [_contentView addSubview:confirmBtn];
    }
    return _contentView;
}

- (UIPickerView *)pickerView {
    if (!_pickerView) {
        _pickerView = [[ UIPickerView alloc] initWithFrame:CGRectMake(0, KScaleWidth(58.0), kScreenWidth, _contentView.height - KScaleWidth(58.0))];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
    }
    return _pickerView;
}

- (UIDatePicker *)datePicker {
    if (!_datePicker) {
        _datePicker = [[UIDatePicker alloc] init];
        _datePicker.frame = CGRectMake((self.contentView.width - _datePicker.width)/2, KScaleWidth(58.0), _datePicker.width, _contentView.height - KScaleWidth(58.0));
        _datePicker.datePickerMode = UIDatePickerModeDate;
        if (@available(iOS 13.4, *)) {
            _datePicker.preferredDatePickerStyle = UIDatePickerStyleWheels;
        } else {
            // Fallback on earlier versions
        }
    }
    return _datePicker;
}

- (void)getYearTypeWithIndex:(NSInteger)index {
    NSInteger selectYear = [_currentYear intValue] - 150 + index + 1;
    if ((selectYear%4 == 0 && selectYear%100 != 0) || selectYear%400 == 0) {
        _isLeapYear = YES;
    }
    else {
        _isLeapYear = NO;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
